package com.xxx.test;

import com.xxx.DAO.commentDao;
import com.xxx.entity.Comment;
import com.xxx.util.JDBCUtil;
import com.xxx.util.pageUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class showContest extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int iPageSize = 4, iTotal = 0, iPageCnt = 0, iPage = 0;
        if (request.getParameter("page") != null) {// 从翻页操作链接进入
            try {
                iPage = Integer.parseInt(request.getParameter("page"));
            } catch (RuntimeException e) {
                e.printStackTrace();
            }
        }
        if (iPage < 1) { iPage = 1; }
        request.setAttribute("iPage",iPage);
        commentDao dao=new commentDao();
        List<Comment> commentsList1 = new ArrayList<>();//存放查询到的数据
        List<Comment> commentsList2 = new ArrayList<>();//存放分页的数据
        pageUtil pageutil=new pageUtil();//创建分页类
        JDBCUtil util=new JDBCUtil();
        commentsList1=dao.findAll();
        iTotal = pageutil.getPageCount(commentsList1);
        iPageCnt=(iTotal+iPageSize-1)/iPageSize;
        if (iPage>iPageCnt) { iPage=iPageCnt; }
        if (iPageCnt>0){
            commentsList2=pageUtil.startPage(commentsList1,iPage,4);
            request.setAttribute("list",commentsList2);
            request.setAttribute("iPageCnt",iPageCnt);
        }

        //跳转页面
        request.getRequestDispatcher("comment.jsp").forward(request,response);
    }
}
